package com.example.backend.controller;

import com.example.backend.entity.User;
import com.example.backend.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

// 用 HTTP 方法 （GET-查、POST-增、PUT-改、DELETE-删） 表示对资源的操作。
@RestController
@CrossOrigin
@Log4j
public class UserController {
    private static User user;
    @Autowired
    private UserService userService;

    /** 注册 */
    @PostMapping("/user")
    public Map<String, Object> doRegister(@RequestBody User user) {
        Map<String, Object> response = new HashMap<>();
        if(userService.register(user)){
            response.put("success", false);
            response.put("message", "该手机号已注册");
            return response;
        }
        int result = userService.addUser(user);
        if (result > 0) {
            response.put("success", true);
            response.put("message", "注册成功");
        } else {
            response.put("success", false);
            response.put("message", "注册失败");
        }
        return response;
    }

    /** 获取登录的用户 */
    @GetMapping("/user1")
    public User doGet(User  user, HttpServletRequest request) {
        System.out.println("获取用户");
        HttpSession session = request.getSession();
        User user1= (User) session.getAttribute("user");
        return user1;
    }
    /** 注销用户 */
    @GetMapping("/user2")
    public User dodele(HttpServletRequest request) {
//        this.user=null;
        HttpSession session = request.getSession();
        session.removeAttribute("user");
        return null;
    }

    /** 登录 */
    @GetMapping("/user")
    public Map<String, Object> doLoginBysjh(User user, HttpServletRequest request) {
        Map<String, Object> response = new HashMap<>();
        if(userService.selectUser(user)!=null){
            response.put("success", true);
            response.put("message", "登录成功");
            // 将用户信息存入session
            HttpSession session = request.getSession();
            session.setAttribute("user", userService.selectUser(user));
//            this.user=userService.selectUser(user);
        } else {
            response.put("success", false);
            response.put("message", "登录失败");
        }
        return response;
    }

    /** 查询所有用户 */
    @GetMapping("/users")
    public Map<String, Object> doselectAll(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "5") int pageSize,
            User user) {
        return userService.selectAll(user, pageNum, pageSize);
    }

    /**更新用户信息*/
    @PutMapping("/user/{id}")
    public Map<String, Object> doUpdateUser(@PathVariable String id, @RequestBody User user, HttpServletRequest request) {
        user.setId(id); // 设置 ID
        int result = userService.updateUser(user);
        Map<String, Object> response = new HashMap<>();
        if (result > 0) {
            response.put("success", true);
            response.put("message", "更新成功");
        } else {
            response.put("success", false);
            response.put("message", "更新失败");
        }
        // 将用户信息存入session
        HttpSession session = request.getSession();
        session.setAttribute("user", userService.selectUser(user));
        return response;
    }
    @DeleteMapping("/user/{id}")
    public Map<String, Object> doDeleteUser(@PathVariable String id) {
        int result = userService.deleteUserById(id);
        Map<String, Object> response = new HashMap<>();
        if (result > 0) {
            response.put("success", true);
            response.put("message", "删除成功");
        } else {
            response.put("success", false);
            response.put("message", "删除失败");
        }
        return response;
    }


}
